package com.huaxin.device.dto.enums;

import com.baomidou.mybatisplus.annotation.EnumValue;
import com.fasterxml.jackson.annotation.JsonValue;
import com.huaxin.hxenum.base.BaseCodeEnum;
import com.huaxin.hxenum.base.Colorful;
import com.huaxin.hxenum.base.Describable;

/**
 * 报警等级
 */
public enum AlarmLevel implements BaseCodeEnum<Integer>, Describable, Colorful {

    CRITICAL(1, "危急", "系统立即停止，需人工干预", "#FF0000"),
    MAJOR(2, "严重", "系统异常，需尽快处理", "#FF4500"),
    MINOR(3, "一般", "轻微异常，可延迟处理", "#FFA500"),
    WARNING(4, "警告", "潜在风险，注意观察", "#FFFF00"),
    INFO(5, "信息", "正常事件记录", "#0000FF");

    @EnumValue
    private final int code;
    private final String displayName;
    private final String description;
    private final String color;

    AlarmLevel(int code, String displayName, String description, String color) {
        this.code = code;
        this.displayName = displayName;
        this.description = description;
        this.color = color;
    }

    @JsonValue
    @Override
    public Integer getCode() {
        return code;
    }

    @Override
    public String getDisplayName() {
        return displayName;
    }

    @Override
    public String getDescription() {
        return description;
    }

    @Override
    public String getColor() {
        return color;
    }

    /**
     * 根据 code 获取枚举实例
     */
    public static AlarmLevel of(int code) {
        for (AlarmLevel level : values()) {
            if (level.code == code) {
                return level;
            }
        }
        return null;
    }

    /**
     * 校验 code 是否有效
     */
    public static boolean isValid(int code) {
        return of(code) != null;
    }

    /**
     * 兼容字符串输入（用于校验）
     */
    public static boolean isValid(String codeStr) {
        if (codeStr == null || codeStr.trim().isEmpty()) {
            return false;
        }
        try {
            int code = Integer.parseInt(codeStr.trim());
            return isValid(code);
        } catch (NumberFormatException e) {
            return false;
        }
    }
}